function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

export function formatTimeOne(time, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
  var returnArr = []
  var timeRes
  if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
    timeRes = parseInt(time);
  }
  if (typeof time === 'number' && time.toString().length === 10) {
    timeRes = time * 1000
  } else {
    timeRes = time
  }
  var date = new Date(timeRes)
  returnArr.push(date.getFullYear())
  returnArr.push(formatNumber(date.getMonth() + 1))
  returnArr.push(formatNumber(date.getDate()))
  returnArr.push(formatNumber(date.getHours()))
  returnArr.push(formatNumber(date.getMinutes()))
  returnArr.push(formatNumber(date.getSeconds()))
  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i])
  }
  return format
}

export function formatTimeTwo(number, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];
  var date = new Date(number * 1000); // s * 1000 => ms
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));
  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));
  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

export function formatTimeThree(fmt, timestamp) {
  const date = timestamp ? new Date(timestamp) ? new Date()
  const o = {
    "Y+": date.getFullYear(),
    "M+": date.getMonth() + 1,     // 月
    "D+": date.getDate(),          // 日
    "h+": date.getHours(),         // 时
    "m+": date.getMinutes(),       // 分
    "s+": date.getSeconds(),       // 秒
    "W": date.getDay()             // 周
  }
  for (var k in o) {
    if (new RegExp("("+ k +")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, () => {
        if (k === 'W') {                                    // 星期几
          const week = ['日', '一', '二', '三', '四', '五', '六']
          return week[o[k]]
        } else if (k === 'Y+' || RegExp.$1.length == 1) {    // 年份 or 小于10不加0
          return o[k]
        } else {
          return ("00"+ o[k]).substr(("" + o[k]).length)  // 小于10补位0
        }
      })
    }
  }
  return fmt
}

export function formatTimeFour (timestamp) {
  const minute = 1000 * 60;
  const hour = minute * 60;
  const day = hour * 24;
  const month = day * 30;
  const year = month * 12;
  const diffValue = new Date().getTime() - timestamp;

  // 如果本地时间小于变量时间
  if (diffValue <= 0) {
    return '现在'
  }

  // 计算差异时间的量级
  const yearC = diffValue / year;
  const monthC = diffValue / month;
  const weekC = diffValue / (7 * day);
  const dayC = diffValue / day;
  const hourC = diffValue / hour;
  const minC = diffValue / minute;

  // 从大到小排序 满足1以上即成立
  const map = {
    [yearC]: '年',
    [monthC]: '月',
    [weekC]: '周',
    [dayC]: '天',
    [hourC]: '小时',
    [minC]: '分钟'
  }
  for (let i in map) {
    if (i >= 1) {
      return `${parseInt(i)}${map[i]}前`
    }
  }
}

/* 微信显示形式 */
export const timeDate = (time, type) => {
  if (String(time).length == 10) time = time * 1000;
  let t = new Date(time);
  let n = new Date();
  let [nY, nM, nD] = [n.getFullYear(), n.getMonth() + 1, n.getDate()];
	let [tY, tM, tD, tH, tMin] = [t.getFullYear(), t.getMonth() + 1, t.getDate(), formatNumber(t.getHours()), formatNumber(t.getMinutes())];
	let date1 = `${tY}-${tM}-${tD}`;
	let date2 = `${nY}-${nM}-${nD}`;
	let weekDay = n.getDay();
  let time1 = new Date(date1).getTime();
  let time2 = new Date(date2).getTime();
  let weekStartDate = new Date(nY, nM - 1, nD - weekDay);
  let weekEndDate = new Date(nY, nM - 1, nD + ( 7 - weekDay));

  if (date1 === date2) { // 当天
    return `${tH}:${tMin}`
  } else if (time1 - time2 < 0 && time1 - time2 >= -24 * 3600000) {
    return type ? `昨天 ${tH}:${tMin}` : '昨天'
  } else if (time1 >= weekStartDate && time1 < weekEndDate ) {
    return type ? `${week(t.getDay())} ${tH}:${tMin}` : `${week(t.getDay())}`
  } else if (tY === nY) {
    return type ? `${tM}-${tD} ${tH}:${tMin}` : `${formatNumber(tM)}-${formatNumber(tD)}`
  } else {
    return type? `${tY}-${tM}-${tD} ${tH}:${tMin}` : `${tY}-${formatNumber(tM)}-${formatNumber(tD)}`
  }
}

export const week = (t) => {
  const dayList = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  return dayList[t];
}